class OrdersController < ApplicationController
  wechat_api


  #订单列表
  def index
    @title = '订单列表'
  end

  #订单初始化
  def new

    @product_source = params[:product_source]

    userinfo_id = current_userinfo.id.to_s
    customer_id = current_wechat_session.customer_id.present? ? current_wechat_session.customer_id : current_wechat_session.business_id
    product_list = params[:product_list]

    req_params = {:userinfo_id => userinfo_id, :customer_id => customer_id, :activities => []}
    activite={:preferential_way => 0, :ordergoods => []}
    product_list.split(",").each do |product|
      product_infos = product.split(":")
      activite[:ordergoods] << {:product_id => product_infos[0], :quantity => product_infos[1]}
    end
    req_params[:activities] << activite

    @result_data = ElephantServiceHelper.post_data("v2,order,select_products_ok", {:order => req_params.to_json})

    Rails.logger.info "选好了接口数据信息为:#{@result_data.to_json}"
    @title = '订单结算'

    if @result_data['state'] == 200
      render 'orders/new'
    else
      render 'orders/new_fail'
    end
  end

  # 订单提交成功
  def success

    @title = '订单结算'
    @order_id = params[:order_id]
  end


  # 订单提交成功
  def show

    order_id = params[:order_id]
    @order_data = ElephantServiceHelper.get_data("v1,order,search_order?", {:orderid => order_id})
    if !@order_data['ordergoods'].present?
      @order_data['ordergoods'] = @order_data['ordergoodcompleteds']
    end
    @title = '订单详细'
  end

  # 跳转到地图
  def goto_map
    @params=params
  end

end